//
//  MYCommonQuestionVC.m
//  MuYing
//
//  Created by tbb on 2019/8/19.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYCommonQuestionVC.h"
#import "MYCommonQuestionModel.h"
@interface MYCommonQuestionVC ()
//@property (nonatomic, copy) NSArray * headerTitleArr;
@end

@implementation MYCommonQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"常见问题");
}

-(void)setupView {
    [super setupView];
    
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYCommonQuestionHeader")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYCommonQuestionCell")
    
    [self getCommonQuestionList];
}

-(void)getCommonQuestionList{
    YQSHOWLOADINGTIPS(@"")
    [YQPNetWorkingManager sendGetRequest:kPOST_SLIDESHOW_FAQ_URL parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            [self.dataArr removeAllObjects];
            NSArray *arr = [NSArray modelArrayWithClass:[MYCommonQuestionModel class] json:responseObject.data];
            MYCommonQuestionModel *model = arr.firstObject;
            model.isShow = YES;
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MYCommonQuestionModel *qusetionModel = nil;
    if (section < self.dataArr.count) {
        qusetionModel = self.dataArr[section];
    }
    if (qusetionModel.isShow) {
        return qusetionModel.faqList.count;
    }else{
        return 0;
    }
    
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYCommonQuestionModel *qusetionModel = nil;
    if (indexPath.section < self.dataArr.count) {
        qusetionModel = self.dataArr[indexPath.section];
    }
    
    MYCommonQuestionConentModel *conentModel = nil;
    
    if (indexPath.row < qusetionModel.faqList.count) {
        conentModel =  qusetionModel.faqList[indexPath.row];
    }
    
    MYCommonQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCommonQuestionCell"];
    cell.qusetionLb.text = conentModel.title;
    cell.contentLb.text = conentModel.content;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYCommonQuestionModel *qusetionModel = nil;
    if (indexPath.section < self.dataArr.count) {
        qusetionModel = self.dataArr[indexPath.section];
    }
    
    MYCommonQuestionConentModel *conentModel = nil;
    
    if (indexPath.row < qusetionModel.faqList.count) {
        conentModel =  qusetionModel.faqList[indexPath.row];
    }
    
    return [tableView fd_heightForCellWithIdentifier:@"MYCommonQuestionCell" configuration:^(MYCommonQuestionCell *cell) {
        cell.qusetionLb.text = conentModel.title;
        cell.contentLb.text = conentModel.content;
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYCommonQuestionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYCommonQuestionHeader"];
    MYCommonQuestionModel *qusetionModel = nil;
    if (section < self.dataArr.count) {
        qusetionModel = self.dataArr[section];
    }
    header.titleLb.text = qusetionModel.name;
    header.selectBtn.tag = 10010 + section;
    [header.selectBtn addTarget:self action:@selector(showAction:) forControlEvents:(UIControlEventTouchUpInside)];
    header.selectBtn.selected = qusetionModel.isShow;
    return header;
}
-(void)showAction:(UIButton *)sender {
    NSInteger section = sender.tag - 10010;
    MYCommonQuestionModel *qusetionModel = nil;
    if (section < self.dataArr.count) {
        qusetionModel = self.dataArr[section];
    }
    qusetionModel.isShow = !qusetionModel.isShow;
    [self.listTableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 58;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

@end

